//
//  TalkModel.m
//  家校互通
//
//  Created by 姬鑫宇 on 15/5/14.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "TalkModel.h"

@implementation TalkModel
/**
 *  21)	创建聊天室(带推送)
 *
 *  @param token   <#token description#>
 *  @param name    <#name description#>
 *  @param classId <#classId description#>
 *  @param userId  <#userId description#>
 */
+ (NSURLSessionDataTask *)createClazzChart:(void (^)(BOOL result,TalkRoomModel *roomList, NSError *error))block withToken:(NSString *)token andName:(NSString *)name andClassId:(NSInteger) classId andUserId:(NSString *) userId{
    NSDictionary * dict = @{@"token":token,@"name":name,@"clazzId":@(classId),@"userIds":userId};
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"chart/createClazzChart" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        NSDictionary * dic = [JSON valueForKeyPath:@"data"];
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        TalkRoomModel *room = nil;
        BOOL result;
        if (code == 10000) {
            result = YES;
            room = [[TalkRoomModel alloc] initWithDictionary:dic error:nil];

        }else
            result = NO;
        
        if (block) {
            block(result,room,nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,nil,error);
        }
    }];
}
+ (NSURLSessionDataTask *)inviteJoinChart:(void (^)(BOOL result,TalkRoomModel *roomList, NSError *error))block withToken:(NSString *)token andChartId:(NSInteger) chartId andUserIds:(NSString *) userIds{
    NSDictionary * dict = @{@"token":token,@"chartId":@(chartId),@"userIds":userIds};
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"chart/inviteJoinChart" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSDictionary * dic = [JSON valueForKeyPath:@"data"];
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        NSMutableArray *chartUsersArray = [[NSMutableArray alloc]initWithCapacity:0];
        NSMutableArray *invateUsersArray = [[NSMutableArray alloc]initWithCapacity:0];
        TalkRoomModel *room = nil;
        BOOL result;
        if (code == 10000) {
            result = YES;
            room = [[TalkRoomModel alloc] initWithDictionary:dic error:nil];
            for (NSDictionary * cdic in [dic objectForKey:@"chart_users"]) {
                TalkUserModel *user = [[TalkUserModel alloc] initWithDictionary:cdic error:nil];
                [chartUsersArray addObject:user];
            }
            for (NSDictionary * cdic in [dic objectForKey:@"invite_join_users"]) {
                TalkUserModel *user = [[TalkUserModel alloc] initWithDictionary:cdic error:nil];
                [invateUsersArray addObject:user];
            }
            room.chart_users=chartUsersArray;
            room.invite_join_users=invateUsersArray;
            
        }else
            result = NO;
        
        if (block) {
            block(result,room,nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,nil,error);
        }
    }];
}
/**
 *  24)	删除聊天室/请假条(带推送)
 *
 *  @param token   <#token description#>
 *  @param chartId <#chartId description#>
 */
+ (NSURLSessionDataTask *)deleteClazzChart:(void (^)(BOOL result,NSError *error))block withToken:(NSString *)token andChartId:(NSInteger) chartId{
    NSDictionary * dict = @{@"token":token,@"chartId":@(chartId)};
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"chart/deleteClazzChart" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        BOOL result;
        if (code == 10000) {
            result = YES;
            
        }else
            result = NO;
        
        if (block) {
            block(result,nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,error);
        }
    }];

}
/**
 *  24)	退出聊天室/请假条(带推送)
 *
 *  @param token   <#token description#>
 *  @param chartId <#chartId description#>
 */
+ (NSURLSessionDataTask *)leaveChart:(void (^)(BOOL result,NSError *error))block withToken:(NSString *)token andChartId:(NSInteger) chartId{
    NSDictionary * dict = @{@"token":token,@"chartId":@(chartId)};
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"chart/leaveChart" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        BOOL result;
        if (code == 10000) {
            result = YES;
            
        }else
            result = NO;
        
        if (block) {
            block(result,nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,error);
        }
    }];
    
}
/**
 *  26)	获取聊天室成员信息
 *
 *  @param token   <#token description#>
 *  @param chartId <#chartId description#>
 */
+ (NSURLSessionDataTask *)getChartUsers:(void (^)(BOOL result,NSMutableArray *userList,NSError *error))block withToken:(NSString *)token andChartId:(NSInteger) chartId{
    NSDictionary * dict = @{@"token":token,@"chartId":@(chartId)};
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"chart/getChartUsers" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        NSArray *responseArray = [JSON valueForKeyPath:@"data"];
        NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        BOOL result;
        if (code == 10000) {
            result = YES;
            for (NSDictionary * dic in responseArray) {
                TalkUserModel *user = [[TalkUserModel alloc] initWithDictionary:dic error:nil];
                [array addObject:user];
            }
        }else
            result = NO;
        
        if (block) {
            block(result,array,nil);
        }

    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,nil,error);
        }
    }];
}

+ (NSURLSessionDataTask *)sendMsg:(void (^)(BOOL result,NSError *error))block andChartId:(NSInteger) chartId andReceiverId:(NSInteger) receiverId andContent:(NSString *)content{
    NSDictionary * dict = @{@"token":[DSFUserInfo shareUserInstance].token,@"chartId":@(chartId),@"content":content};
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:dict];
    if (receiverId) {
        [dic setObject:@(receiverId) forKey:@"receiverId"];
    }
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"message/sendMsg" parameters:dic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        BOOL result;
        if (code == 10000) {
            result = YES;
        }else
            result = NO;
        
        if (block) {
            block(result,nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,error);
        }
    }];

}

+ (NSURLSessionDataTask *)sendImg:(void (^)(BOOL, NSError *))block andChartId:(NSInteger)chartId andReceiverId:(NSInteger)receiverId andContent:(NSString *)content{
    NSDictionary * dict = @{@"token":[DSFUserInfo shareUserInstance].token,@"chartId":@(chartId),@"content":content};
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:dict];
    if (receiverId) {
        [dic setObject:@(receiverId) forKey:@"receiverId"];
    }
    return [[AFAppDotNetAPIClient httpSharedClient] POST:@"message/sendImg" parameters:dic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        long code = [[JSON valueForKeyPath:@"code"] integerValue];
        BOOL result;
        if (code == 10000) {
            result = YES;
        }else
            result = NO;
        
        if (block) {
            block(result,nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block(NO,error);
        }
    }];
    
}


@end

@implementation TalkRoomModel

@end

@implementation TalkUserModel

@end
